//
//  YZMResultView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/5.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMResultView.h"
#import <BlocksKit/UIControl+BlocksKit.h>

@implementation YZMResultView{
    
    UIImageView *_iconImage;
    UILabel *_recommandLabel;
    UIActivityIndicatorView *_indicatorView;
    UILabel *_detailLabel;
    
    UIButton *_retryButton;
    YZM_RESULT_STATUS _resultStatus;
    
    UIView *_contentView;
}

- (id)init{
    
    self = [super init];
    if (self) {
        
        [self createObjects];
        self.status = YZM_RESULT_STATUS_FOR_NORMAL;
    }
    
    return self;
}

- (void)createObjects{
    
    _contentView = [UIView new];
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.center.equalTo(self);
    }];
    
    _recommandLabel = [YZMAppStyle labelWithFont:[YZMAppStyle largeSizeFont] andColor:[YZMAppStyle colorForMainTextColor]];
    _recommandLabel.textAlignment = NSTextAlignmentCenter;
    _recommandLabel.numberOfLines = 0;
    [_contentView addSubview:_recommandLabel];
    [_recommandLabel mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.equalTo(_contentView);
        maker.top.equalTo(_contentView);
    }];
    
    _iconImage = [UIImageView new];
    _iconImage.image = [UIImage imageNamed:@"icon_no_wifi"];
    [_contentView addSubview:_iconImage];
    [_iconImage mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.width.mas_equalTo(103);
        maker.height.mas_equalTo(103);
    }];
    
    _detailLabel = [YZMAppStyle labelWithSmallLightGray];
    _detailLabel.numberOfLines = 0;
    _detailLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:_detailLabel];
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.equalTo(_contentView);
    }];
    
    _retryButton = [UIButton new];
    _retryButton.backgroundColor = [UIColor clearColor];
    [_retryButton setTitle:@"重试" forState:UIControlStateNormal];
    [_retryButton setTitleColor:[YZMAppStyle colorForRiseText] forState:UIControlStateNormal];
    [_retryButton.titleLabel setFont:[YZMAppStyle mediumSizeFont]];
    _retryButton.layer.cornerRadius = 8;
    _retryButton.layer.masksToBounds = YES;
    _retryButton.layer.borderColor = [YZMAppStyle colorForRiseText].CGColor;
    _retryButton.layer.borderWidth = 1;
    [_retryButton bk_addEventHandler:^(id sender) {
        
        if (_retryAction) {
            
            _retryAction();
        }
        
    } forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_retryButton];
    [_retryButton mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.centerX.equalTo(_contentView);
        maker.width.mas_equalTo(140);
        maker.height.mas_equalTo(40);
    }];
    
    _indicatorView = [UIActivityIndicatorView new];
    _indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [_contentView addSubview:_indicatorView];
    [_indicatorView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.width.height.mas_equalTo(12);
    }];
    
}

- (void)setStatus:(YZM_RESULT_STATUS)status{
    
    _status = status;
    
    switch (_status) {
        case YZM_RESULT_STATUS_FOR_NORMAL: {
            [self refreshNormal];
            break;
        }
        case YZM_RESULT_STATUS_FOR_LOADING: {
            [self refreshLoading];
            break;
        }
        case YZM_RESULT_STATUS_FOR_NO_WIFI: {
            [self refreshNoWifi];
            break;
        }
        case YZM_RESULT_STATUS_FOR_ABNORMAL: {
            [self refreshAbnormal];
            break;
        }
        case YZM_RESULT_STATUS_FOR_NO_DATA: {
            [self refreshNoData];
            break;
        }
        default: {
            break;
        }
    }
}

- (void)refreshNormal{
    
    if (_indicatorView && [_indicatorView isAnimating]) {
        
        [_indicatorView stopAnimating];
    }
    
    self.hidden = YES;
}

- (void)refreshLoading{
    
    self.hidden = NO;
    
    _recommandLabel.hidden = NO;
    _iconImage.hidden = NO;
    _indicatorView.hidden = NO;
    
    _detailLabel.hidden = YES;
    [_detailLabel mas_remakeConstraints:^(MASConstraintMaker *maker){
        
    }];
    
    _retryButton.hidden = YES;
    [_retryButton mas_remakeConstraints:^(MASConstraintMaker *maker){
        
    }];
    
    _iconImage.image = [UIImage imageNamed:@"icon_loading_bg"];
    [_iconImage mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.centerX.equalTo(_contentView);
        maker.width.mas_equalTo(95);
        maker.height.mas_equalTo(95);
    }];
    
    _recommandLabel.text = @"数据加载中！";
    [_recommandLabel mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.centerY.equalTo(_indicatorView);
        maker.centerX.equalTo(_contentView).offset(20);
    }];
    
    [_indicatorView startAnimating];
    [_indicatorView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(_iconImage.mas_bottom).offset(PAGECONTENTMARGIN);
        maker.right.equalTo(_recommandLabel.mas_left).offset(-10);
        maker.width.height.mas_equalTo(12);
    }];
    
    [_contentView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.equalTo(self);
        maker.centerY.equalTo(self);
        maker.bottom.equalTo(_indicatorView);
    }];
}

- (void)refreshNoWifi{
    
    self.hidden = NO;
    
    _recommandLabel.hidden = NO;
    _iconImage.hidden = NO;
    _retryButton.hidden = NO;
    
    _detailLabel.hidden = YES;
    _indicatorView.hidden = YES;
    [_indicatorView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
    }];
    
    if (_indicatorView && [_indicatorView isAnimating]) {
        
        [_indicatorView stopAnimating];
    }
    
    _iconImage.image = [UIImage imageNamed:@"icon_no_wifi"];
    [_iconImage mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.centerX.equalTo(_contentView);
        maker.width.mas_equalTo(103);
        maker.height.mas_equalTo(103);
    }];
    
    _recommandLabel.text = @"网络异常，请检查您的网络";
    [_recommandLabel mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(_iconImage.mas_bottom).offset(40);
        maker.left.right.equalTo(_contentView);
    }];
    
    [_retryButton setTitle:@"重试" forState:UIControlStateNormal];
    [_retryButton mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(_recommandLabel.mas_bottom).offset(20);
        maker.centerX.equalTo(_contentView);
        maker.width.mas_equalTo(100);
        maker.height.mas_equalTo(40);
    }];
    
    [_contentView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.equalTo(self);
        maker.centerY.equalTo(self);
        maker.bottom.equalTo(_retryButton);
    }];
}

- (void)refreshAbnormal{
    
    [self refreshNoWifi];
}

- (void)refreshNoData{
    
    self.hidden = NO;
    
    _recommandLabel.hidden = NO;
    _iconImage.hidden = NO;
    
    _indicatorView.hidden = YES;
    [_indicatorView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
    }];
    _detailLabel.hidden = YES;
    [_detailLabel mas_remakeConstraints:^(MASConstraintMaker *maker){
        
    }];
    _retryButton.hidden = YES;
    [_retryButton mas_remakeConstraints:^(MASConstraintMaker *maker){
        
    }];
    
    if (_indicatorView && [_indicatorView isAnimating]) {
        
        [_indicatorView stopAnimating];
    }
    
    _iconImage.image = [UIImage imageNamed:@"icon_no_record"];
    [_iconImage mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.centerX.equalTo(_contentView);
        maker.width.mas_equalTo(92);
        maker.height.mas_equalTo(92);
    }];
    
    _recommandLabel.text = @"暂无数据";
    [_recommandLabel mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(_iconImage.mas_bottom).offset(PAGECONTENTMARGIN);
        maker.left.right.equalTo(_contentView);
    }];
    
    [_contentView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.equalTo(self);
        maker.centerY.equalTo(self);
        maker.bottom.equalTo(_recommandLabel);
    }];
}

- (void)syncStatusWithError:(NSError *)error
{
    if (error.code == -1009) {
        self.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
    }
    else
    {
        self.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
    }
}

@end
